#!/bin/bash
get_latest_release() {
	curl --silent "https://api.github.com/repos/$repo/releases/latest" | # Get latest release from GitHub api
		grep '"tag_name":' |                                            # Get tag line
		sed -E 's/.*"([^"]+)".*/\1/'                                    # Pluck JSON value
	}

repo='huacnlee/autocorrect'
bin_name='autocorrect'
version=`get_latest_release`

echo "Downloading $bin_name@$version ..."

if [[ `uname` == 'Darwin' ]]; then
	platform='darwin'
else
	platform='linux'
fi
if [[ `arch` == 'arm64' ]]; then
	arch='arm64'
else
	arch='amd64'
fi
curl -Lo $bin_name.tar.gz https://github.com/$repo/releases/download/$version/$bin_name-$platform-$arch.tar.gz
tar zxf $bin_name.tar.gz

if [[ `whoami` == 'root' ]]; then
	mv $bin_name /usr/local/bin/$bin_name
else
	sudo mv $bin_name /usr/local/bin/$bin_name
fi
rm $bin_name.tar.gz
echo "AutoCorrect $version has installed successfully."
